import { inject, injectable } from 'inversify';
import { UserModel } from '../models/user.model.ts';
import { UserRepository } from '../data/user.repository.ts';

@injectable()
export class GetUserProfileUseCase {
  constructor(
    @inject(UserRepository)
    private userRepository: UserRepository,
    @inject(UserModel)
    private userModel: UserModel,
  ) {}

  async execute(): Promise<void> {
    try {
      this.userModel.loading = true;
      const result = await this.userRepository.getProfile();

      this.userModel.initialize({
        _initialized: true,
        authenticated: true,
        id: result.data._id,
        username: result.data.username || '',
        email: result.data.email,
        mobileNumber: result.data.mobileNumber || '',
        firstName: result.data.firstName,
        lastName: result.data.lastName,
        signUpDate: result.data.signUpDate,
        signUpFrom: result.data.signUpFrom,
        createdAt: result.data.createdAt,
        updatedAt: result.data.updatedAt,
        role: {
          _id: result.data.role._id,
          name: result.data.role.name,
          description: result.data.role.description,
          isActive: result.data.role.isActive,
          type: result.data.role.type,
          permissions: result.data.role.permissions,
          createdAt: result.data.role.createdAt,
          updatedAt: result.data.role.updatedAt,
        },
        photo: result.data.photo || null,
      });
    } finally {
      this.userModel.loading = false;
    }
  }
}
